//
//  InstructionViewController.m
//  Gruphoto
//
//  Created by Thao Nguyen Huy on 1/14/13.
//  Copyright (c) 2013 CNC Software. All rights reserved.
//

#import "InstructionViewController.h"
#import "GruphotoTabBarController.h"

@interface InstructionViewController ()
- (void)swipeLeft;
- (void)swipeRight;
- (void)doubleTap;
@end

@implementation InstructionViewController
@synthesize instructionScrollView, instructionIndex, instructionImageView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:226/255.0 green:231/255.0 blue:237/255.0 alpha:1.0];
    NSString *imageName = [NSString stringWithFormat:@"instruction_page%d.png", instructionIndex];
    instructionImageView.image = [UIImage imageNamed:imageName];
    
    UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft)];
    leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:leftSwipe];
    [leftSwipe release];
    UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRight)];
    rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:rightSwipe];
    [rightSwipe release];
    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap)];
    doubleTap.numberOfTapsRequired = 2;
    [self.view addGestureRecognizer:doubleTap];
    [doubleTap release];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.instructionScrollView = nil;
    self.instructionImageView = nil;
}

- (void)dealloc {
    [instructionScrollView release];
    [instructionImageView release];
    [super dealloc];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Private methods
- (void)swipeLeft {
    NSLog(@"Left: %d", instructionIndex);
    if (instructionIndex < 3) {
        InstructionViewController *instructionViewController = [[InstructionViewController alloc] initWithNibName:@"InstructionViewController" bundle:nil];
        instructionViewController.instructionIndex = instructionIndex + 1;
        [self.navigationController pushViewController:instructionViewController animated:YES];
        [instructionViewController release];
    }
    else {
        [self doubleTap];
    }
}

- (void)swipeRight {
    if (instructionIndex > 1) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)doubleTap {
    GruphotoTabBarController *tabbarController = [[GruphotoTabBarController alloc] init];
    [self presentModalViewController:tabbarController animated:YES];
    [tabbarController release];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

@end
